﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Common;
using Common.Admin;
using Common.Controls;

namespace SLAdmin.UserControls
{
    public partial class EditStudyWave : UserControl
    {
        StudyWave _studyWave;
        int _studyWaveId;
        Condition _condition;
        int _conditionId;
        App _myApp = App.GetApplication;
        StudyManager _studyManager = StudyManager.GetInstance();
        StudyDataClient _studyDataClient = new StudyDataClient();
        DialogHelper _dialogHelper = new DialogHelper();

        public delegate void StudyWaveUpdatedDelegate();
        public event StudyWaveUpdatedDelegate StudyWaveUpdated;
        public EditStudyWave()
        {
            InitializeComponent();

        }
        public EditStudyWave(StudyWave myWave, Condition myCondition)
        {
            InitializeComponent();

            _studyDataClient.CreateSurveyCompleted += new StudyDataClient.CreateSurveyCompletedDelegate(_myApp_CreateSurveyCompleted);
            _studyDataClient.AssociateSurveyCompleted += new StudyDataClient.AssociateSurveyCompletedDelegate(_myApp_AssociateSurveyCompleted);
            _dialogHelper.DialogAccepted += new DialogHelper.DialogAcceptedDelegate(_myApp_DialogAccepted);
            _studyDataClient.RemoveSurveyFromWaveCompleted += new StudyDataClient.RemoveSurveyFromWaveCompletedDelegate(_myApp_RemoveSurveyFromWaveCompleted);
    
 
            _studyWave = myWave;
            _studyWaveId = myWave.StudyWaveId;
            _condition = myCondition;
            _conditionId = myCondition.ConditionId;
            this.DataContext = myWave;            
            SurveyGrid.ItemsSource = _studyDataClient.GetSurveys(_studyWaveId, _conditionId);
            List<Survey> Surveys = _studyManager.Surveys;
            ddlSurveys.Items.Clear();
            foreach (Survey s in Surveys)
            {                
                ddlSurveys.Items.Add(s);
            }
        }

        void _myApp_RemoveSurveyFromWaveCompleted(Survey mySurvey)
        {
            SurveyGrid.SelectedItem = null;
            List<Survey> Surveys = SurveyGrid.ItemsSource as List<Survey>;
            Surveys.Remove(mySurvey);
            SurveyGrid.ItemsSource = null;
            SurveyGrid.ItemsSource = Surveys;            
            StudyWaveUpdated();
        }

        void _myApp_AssociateSurveyCompleted(StudyWaveSurvey sws, Survey Survey)
        {
            SurveyGrid.SelectedItem = null;
            List<Survey> Surveys = SurveyGrid.ItemsSource as List<Survey>;
            Surveys.Add(Survey);
            SurveyGrid.ItemsSource = null;
            SurveyGrid.ItemsSource = Surveys;            
            StudyWaveUpdated();
        }

        void _myApp_DialogAccepted(object DataContext)
        {
            if (DataContext is Survey)
            {
                _studyDataClient.RemoveSurveyFromWave(_studyWaveId, _conditionId, DataContext as Survey);
            }
        }

        void _myApp_CreateSurveyCompleted(Survey Survey)
        {
            SurveyGrid.SelectedItem = null;
            List<Survey> Surveys = SurveyGrid.ItemsSource as List<Survey>;
            Surveys.Add(Survey);
            SurveyGrid.ItemsSource = null;
            SurveyGrid.ItemsSource = Surveys;            
            StudyWaveUpdated();
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            _studyDataClient.UpdateStudyWave(_studyWave);
            List<Survey> surveys = SurveyGrid.ItemsSource as List<Survey>;
            foreach (Survey survey in surveys)
            {
                _studyDataClient.UpdateSurvey(survey);
            }
            _studyDataClient.UpdateStudyWaveSurveys(_studyWaveId, _conditionId, surveys);
            StudyWaveUpdated();
        }

        private void Up_Click(object sender, RoutedEventArgs e)
        {
            SurveyGrid.SelectedItem = null;
            List<Survey> list = SurveyGrid.ItemsSource as List<Survey>;
            Button MyButton = (Button)sender;
            Survey mySurvey = (Survey)MyButton.DataContext;
            if (list.IndexOf(mySurvey) != 0)
            {
                list.Reverse(list.IndexOf(mySurvey) - 1, 2);
                SurveyGrid.ItemsSource = null;
                SurveyGrid.ItemsSource = list;                
            }
        }

        private void Down_Click(object sender, RoutedEventArgs e)
        {
            SurveyGrid.SelectedItem = null;
            List<Survey> list = SurveyGrid.ItemsSource as List<Survey>;
            Button MyButton = (Button)sender;
            Survey mySurvey = (Survey)MyButton.DataContext;
            if (list.IndexOf(mySurvey) != list.Count - 1)
            {
                list.Reverse(list.IndexOf(mySurvey), 2);
                SurveyGrid.ItemsSource = null;
                SurveyGrid.ItemsSource = list;                
            }
        }

        private void Delete_Click(object sender, RoutedEventArgs e)
        {
            Button MyButton = (Button)sender;
            Survey mySurvey = (Survey)MyButton.DataContext;
            _dialogHelper.Alert("Confirm remove", "Are you sure you want to remove this survey?",mySurvey,LayoutRoot);                        
        }

        private void btnCreateSurvey_Click(object sender, RoutedEventArgs e)
        {            
            _studyDataClient.CreateSurvey(_studyWaveId, _conditionId);
            
        }


        private void SurveyAdd_Click(object sender, RoutedEventArgs e)
        {
            Survey NewSurvey = ddlSurveys.SelectedItem as Survey;
            _studyDataClient.AssociateSurvey(_studyWaveId, _conditionId, NewSurvey);
            
        }

        private void UserControl_LayoutUpdated(object sender, EventArgs e)
        {
            StudyTabs.Width = this.ActualWidth;
        }

    }
}
